package ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

import data.BankDatabase;

/**
 * @author Matthias
 * @author NielsChristianMouritsen
 */
public class ChoiceAcc extends JPanel{
	
	private final JLabel lbl_Header;
	private final JButton btn_Logout;
	private final JComboBox<String> comboBox_Accounts;
	private final JButton btn_Login;
	private final JLabel lbl_Intro;
	private final JButton btn_UserMenu;
	private final JLabel lbl_Info;
	private final JTextPane textPane_CurrentUser;
	private final JLabel logo_ChooseAcc;
	
	/** Declaring BankDatabase object **/
	private final BankDatabase bdb;
	/** Declaring Screen object **/
	private final Screen screen;
	
	/** Contains the information/blueprint on labels, buttons, combo boxes and text panes
	 * Sets the window dimension and layout
	 * @param screen 	is implemented in the class to access Screen 
	 */
	public ChoiceAcc(final Screen screen) {
		
		this.screen = screen;
		this.bdb = screen.getClassATM().getClassBankDatabase();
		
		setPreferredSize(new Dimension(600, 480));
		setLayout(null);

		lbl_Header = new JLabel("Choose an Account");
		lbl_Header.setFont(new Font("SansSerif", Font.BOLD, 26));
		lbl_Header.setBounds(173, 11, 243, 34);
		add(lbl_Header);

		btn_Logout = new JButton("Logout");
		btn_Logout.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_Logout.setBounds(48, 331, 150, 50);
		add(btn_Logout);
		
		comboBox_Accounts = new JComboBox<String>();
		comboBox_Accounts.setFont(new Font("SansSerif", Font.PLAIN, 16));
		comboBox_Accounts.setBounds(95, 160, 321, 53);
		addAllAcc();
		add(comboBox_Accounts);
		
		btn_Login = new JButton("Login");
		btn_Login.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_Login.setBounds(426, 161, 110, 53);
		add(btn_Login);
		
		lbl_Intro = new JLabel("Which Account do you want to enter?");
		lbl_Intro.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Intro.setBounds(95, 120, 286, 21);
		add(lbl_Intro);
		
		btn_UserMenu = new JButton("Enter User Menu");
		btn_UserMenu.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_UserMenu.setBounds(386, 331, 150, 50);
		add(btn_UserMenu);
		
		lbl_Info = new JLabel("For managing all your accounts");
		lbl_Info.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Info.setBounds(295, 299, 241, 21);
		add(lbl_Info);
		
		textPane_CurrentUser = new JTextPane();
		SimpleAttributeSet attributes = new SimpleAttributeSet();
		attributes.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.TRUE);
		StyleConstants.setFontSize(attributes, 16);
		StyleConstants.setFontFamily(attributes, "SansSerif");
		StyleConstants.setAlignment(attributes , StyleConstants.ALIGN_CENTER);  
		textPane_CurrentUser.setParagraphAttributes(attributes,true); 
		textPane_CurrentUser.setEditable(false);
		textPane_CurrentUser.setOpaque(false);
		textPane_CurrentUser.setBackground(new Color(0,0,0,0));
		textPane_CurrentUser.setBounds(202, 56, 178, 60);
		add(textPane_CurrentUser);
		
		logo_ChooseAcc = new JLabel("");
		logo_ChooseAcc.setIcon(new ImageIcon(ChoiceAcc.class.getResource("/pic/LOGO Mini.png")));
		logo_ChooseAcc.setBounds(15, 15, 50, 50);
		add(logo_ChooseAcc);
	}
	
	/** Adds action listeners to the components
	 * @param e
	 */
	public final void addActionListener(final ActionListener e) {
		btn_Logout.addActionListener(e);
		btn_UserMenu.addActionListener(e);
		btn_Login.addActionListener(e);
		comboBox_Accounts.addActionListener(e);
	}
	
	/** Shows ID, first name and last name on the accounts that you can choose from
	 * @param CPR_ID
	 * @param Fname
	 * @param Lname
	 */
	public void showField(final int CPR_ID, final String Fname, final String Lname){
		textPane_CurrentUser.setText(CPR_ID + "\n" + Fname
				+ " " + Lname);
	}
	
	/**
	 * @return back button
	 */
	public final JButton getBack() {
		return btn_Logout;
	}
	
	/**
	 * @return login button
	 */
	public JButton getLogin(){
		return btn_Login;
	}
	
	/**
	 * @return user menu button
	 */
	public JButton getUserMenu(){
		return btn_UserMenu;
	}
	
	/**
	 * @return combo box, containing the users account 
	 */
	public JComboBox getComboBox() {
		return comboBox_Accounts;
	}

	/**
	 * adding all accounts to the combobox, by using a for loop
	 * in order to retrieve the data from the arraylist. For more information
	 * see the method getAccountChanger()
	 */
	public void addAllAcc(){
		comboBox_Accounts.removeAllItems();	
		String account[] = bdb.getClassUser().getAccountChanger();
		for(int i = 0; i < account.length;i++){
			comboBox_Accounts.addItem(account[i]);		
		}
		screen.pack();
	}
	
	/**
	 * @return the specific account nr of the chosen item in the combobox
	 * again using a for loop, in order to retrieve the data, by inserting the selected index
	 * from the combobox. Used in the class Transaction, in order to do account actions such as deposit
	 */
	public int getSpecificAccNr() {
		int[] first = new int[bdb.getClassUser().getAccountList().size()];
	    for (int i = 0; i < bdb.getClassUser().getAccountList().size(); i++) {
			first[i] = bdb.getClassUser().getAccountList().get(i).getAccountNumber();
		}
	    return first[comboBox_Accounts.getSelectedIndex()];
	}
	
	/** 
	 * @return the specific account type of the chosen item in the combobox
	 * again using a for loop, in order to retrieve the data, by inserting the selected index
	 * from the combobox. Used in the class Transaction, in order to do account actions such as deposit
	 */
	public String getSpecificAccType() {
		String[] first = new String[bdb.getClassUser().getAccountList().size()];
	    for (int i = 0; i < bdb.getClassUser().getAccountList().size(); i++) {
			first[i] = bdb.getClassUser().getAccountList().get(i).getAccountType();
		}
	    return first[comboBox_Accounts.getSelectedIndex()];
	}
	
} // end class ChoiceAcc